var fmg = {};

fmg.lo1 = null;

function fmg_initLayout() {
	/*var lo = new dhtmlXLayoutObject("maindiv", "1C");
	lo.cells('a').hideHeader();
	lo.cells('b').hideHeader();
	lo.cells('b').setHeight(50);

	var str2 = [
		   		{ type:"block" , name:"form_block_1", offsetLeft:(dhx_skin=="dhx_web"?"333":"343"), offsetTop:"0", list:[
		   		{ type:"button" , name:"fb_ok", value:fmg_i18n_b_ok, inputTop:0  }
		   		]  }
		   	];
	var fm2 = lo.cells('b').attachForm(str2);*/
	fmg.lo1 = new dhtmlXLayoutObject("maindiv", "2U");
	fmg.lo1.cells('a').setText(fmg_i18n_prop_form);
	fmg.lo1.cells('b').setText(fmg_i18n_prop_version);
	fmg.lo1.setAutoSize("a;b", "a;b");
	fmg.lo1.cells('a').setWidth(400);

	fmg_initFormgrid();
	fmg_initVersiongrid();

	/*fm2.attachEvent("onButtonClick", function(btid) {
		if (btid=='fb_ok') {
			var rid = fmg.vergrid.getSelectedRowId();
			if (!rid) {
				msg(fmg_i18n_msg_plsselformver);
				return;
			}
			fmg_returnBindingForm(rid);
		}
	});*/
}

//grid for form layout
function fmg_initFormgrid() {
	fmg.stb = fmg.lo1.cells('a').attachStatusBar();
	fmg.stb.setText("<div id='id_paging_stb1'></div>");
	
	fmg.formgrid = fmg.lo1.cells('a').attachGrid();
	fmg.formgrid.setIconsPath(dhtmlx.image_path);
	fmg.formgrid.setHeader([fmg_i18n_l_no,"FORMID",fmg_i18n_prop_form_type,fmg_i18n_prop_form_name,fmg_i18n_prop_form_desc,fmg_i18n_prop_form_creator,fmg_i18n_prop_form_creatorname,"OP"],null,["text-align:center;","text-align:center;","text-align:center","text-align:center;","text-align:center;","text-align:center;","text-align:center;","text-align:center;"]);
	fmg.formgrid.attachHeader("#rspan,&nbsp;,#rspan,<input id='sr_formname' type='text' style='width:90%'/>,<input id='sr_formdesc' type='text' style='width:90%'/>,#rspan,#rspan,#rspan");
	fmg.formgrid.setInitWidths("40,200,80,150,250,60,80,100");
	fmg.formgrid.setColumnIds("ROW_NO,FORMID,FORMTYPE,FORMNAME,FORMDESC,CREATOR,CREATORNAME,OP");
	fmg.formgrid.setColTypes("ro,ro,pairro,ro,ro,ro,ro,ro");
	fmg.formgrid.setColSorting("na,str,str,str,str,str,str,str");
	fmg.formgrid.setColumnsVisibility("false,true,true,false,false,true,false,true");
	fmg.formgrid.setColAlign("center,left,center,left,left,left,center,left");
	fmg.formgrid.setEditable(false);
	fmg.formgrid.setLang(fmg.lang);
	//default ordering
	fmg.formgrid._grid_ordercolumns = [{"CREATETIME":"DESC"}];
	
	//paging
	fmg.formgrid.setPagingWTMode(true,true,true,[10,20,50,100]);
	fmg.formgrid.enablePaging(true,10,5,'id_paging_stb1');
	fmg.formgrid.setPagingSkin("toolbar", dhx_skin);
	
	fmg.formgrid.init();
	
	fmg.formgrid.attachEvent("onPageChanged", function(ind,fInd,lInd){
		fmg_toPagef(fInd+1, lInd);
	});
	
	fmg.formgrid.attachEvent("onSelectStateChanged", function(grId){
		fmg_toPagev(1, 10);
	});
	
	//header click event
	fmg.formgrid.attachEvent("onHeaderClick", grid_onHeaderClick);
	
	//bind searcher event
	//disable onclick order by function for this header
	var tdsr_formname = $("#sr_formname").parents('td:first');
	var tdsr_formdesc = $("#sr_formdesc").parents('td:first');
	var tdsr_formdescnext = $("#sr_formdesc").parents('td:first').next();
	tdsr_formname.bind('click',fmg_stopEvent);
	tdsr_formdesc.bind('click',fmg_stopEvent);
	tdsr_formdescnext.bind('click',fmg_stopEvent);
	tdsr_formname.bind('mouseover',fmg_stopEvent);
	tdsr_formdesc.bind('mouseover',fmg_stopEvent);
	tdsr_formdescnext.bind('mouseover',fmg_stopEvent);
	$("#sr_formname").bind('keypress', fmg_enterQueryf);
	$("#sr_formdesc").bind('keypress', fmg_enterQueryf);
}

//grid for version layout
function fmg_initVersiongrid() {
	fmg.vtb = fmg.lo1.cells('b').attachStatusBar();
	fmg.vtb.setText("<div id='id_paging_stb2'></div>");
	
	fmg.vergrid = fmg.lo1.cells('b').attachGrid();
	fmg.vergrid.setIconsPath(dhtmlx.image_path);
	fmg.vergrid.setHeader([fmg_i18n_l_no,"VERSIONID",fmg_i18n_prop_form_name,fmg_i18n_prop_form_desc,fmg_i18n_prop_form_updatetime,fmg_i18n_prop_form_changeuserid,fmg_i18n_prop_form_changeusername,fmg_i18n_prop_form_checkoutby,fmg_i18n_prop_form_checkoutbyname,fmg_i18n_prop_form_creator,fmg_i18n_prop_form_creatorname,fmg_i18n_prop_form_status,"OP"],
			null,
			["text-align:center;","text-align:left;","text-align:left","text-align:left","text-align:center;","text-align:center;","text-align:center;","text-align:center;","text-align:center;","text-align:center;","text-align:center;","text-align:center;","text-align:center;"]);
	fmg.vergrid.attachHeader("#rspan,#rspan,<input id='sr_versionname' type='text' style='width:90%'/>,<input id='sr_versiondesc' type='text' style='width:90%'/>,#rspan,#rspan,#rspan,#rspan,#rspan,#rspan,#rspan,#rspan,#rspan");
	fmg.vergrid.setInitWidths("40,100,150,250,120,80,80,80,80,80,80,80,100");
	fmg.vergrid.setColumnIds("ROW_NO,VERSIONID,VERSIONNAME,VERSIONDESC,UPDATETIME,CHANGEUSERID,CHANGEUSERNAME,CHECKOUTBY,CHECKOUTBYNAME,CREATOR,CREATORNAME,STATUS,OP");
	fmg.vergrid.setColTypes("ro,ro,ro,ro,ro,ro,ro,ro,ro,ro,ro,pairro,ro");
	fmg.vergrid.setColSorting("na,str,str,str,str,str,str,str,str,str,str,str,str");
	fmg.vergrid.setColumnsVisibility("false,true,false,false,false,true,false,true,false,true,false,true,true");
	fmg.vergrid.setColAlign("center,left,left,left,center,center,center,center,center,center,center,center,center");
	fmg.vergrid.setEditable(false);
	fmg.vergrid.setLang(fmg.lang);
	fmg.vergrid.init();
	
	//default order by
	//fmg.vergrid._grid_ordercolumns = [{"CREATETIME":"DESC"}];
	
	//paging
	fmg.vergrid.setPagingWTMode(true,true,true,[10,20,50,100]);
	fmg.vergrid.enablePaging(true,10,5,'id_paging_stb2');
	fmg.vergrid.setPagingSkin("toolbar", dhx_skin);
	
	fmg.vergrid.attachEvent("onPageChanged", function(ind,fInd,lInd){
		fmg_toPagev(fInd+1, lInd);
	});
	
	//header click event
	fmg.vergrid.attachEvent("onHeaderClick", grid_onHeaderClick);
	
	fmg.vergrid.attachEvent("onRowDblClicked", function(grId, gcInd){
		//fmg_returnBindingForm(grId);
	});
	
	//bind searcher event
	//disable onclick order by function for this header
	var trsr_versionname = $("#sr_versionname").parents('tr:first');
	trsr_versionname.find('td').bind('click',fmg_stopEvent);
	trsr_versionname.find('td').bind('mouseover',fmg_stopEvent);
	$("#sr_versionname").bind('keypress', fmg_enterQueryv);
	$("#sr_versiondesc").bind('keypress', fmg_enterQueryv);
}

//query form
function fmg_toPagef(fromRow, toRow){
	fmg.lo1.cells('a').progressOn();
	var p = {"form":{"formtype":"PROCESS"}, "fromRow":fromRow, "toRow":toRow, "timezoneOffset":new Date().getTimezoneOffset(),
			"ordercolumns":JSON.stringify(fmg.formgrid._grid_ordercolumns)};
	if (fmg.fquery_formname) {
		p.form.formname = fmg.fquery_formname;
	}
	if (fmg.fquery_formdesc) {
		p.form.formdesc = fmg.fquery_formdesc;
	}
	p.form = JSON.stringify(p.form);
	var ret = doPostSyncJson(fmg.rest_queryf, p);
	if (!ret) {
		fmg.lo1.cells('a').progressOff();
		msgErr(fmg_i18n_msg_readfail);
		return;
	}
	fmg.formgrid.clearAllInPaging();
	fmg.formgrid.parse(ret, "json");
	fmg.vergrid.clearAllInPaging();
	fmg.lo1.cells('a').progressOff();
}

//call query form
function fmg_enterQueryf(e) {
	if (e.keyCode==13) {
		fmg.fquery_formname = $.trim($("#sr_formname").val());
		fmg.fquery_formdesc = $.trim($("#sr_formdesc").val());
		fmg_toPagef(1, 10);
	}
}

//query version
function fmg_toPagev(fromRow, toRow){
	fmg.lo1.cells('b').progressOn();
	var p = {"args":{"excludeCurrent":"1"}, "fromRow":fromRow, "toRow":toRow, "timezoneOffset":new Date().getTimezoneOffset(),
			"ordercolumns":JSON.stringify(fmg.vergrid._grid_ordercolumns)};
	p.args.formid = fmg.formgrid.cells(fmg.formgrid.getSelectedRowId(), fmg.formgrid.getColIndexById("FORMID")).getValue();
	if (fmg.vquery_versionname) {
		p.args.versionname = fmg.vquery_versionname;
	}
	if (fmg.vquery_versiondesc) {
		p.args.versiondesc = fmg.vquery_versiondesc;
	}
	p.args = JSON.stringify(p.args);
	var ret = doPostSyncJson(fmg.rest_queryv, p);
	if (!ret) {
		fmg.lo1.cells('b').progressOff();
		msgErr(fmg_i18n_msg_readfail);
		return;
	}
	fmg.vergrid.clearAllInPaging();
	fmg.vergrid.parse(ret, "json");
	//hide current version display
	var sr = fmg.vergrid.findCell("0",fmg.vergrid.getColIndexById("VERSIONID"),true);
	if (sr && sr.length && sr[0].length) {
		fmg.vergrid.setRowHidden(sr[0][0]);
	}
	fmg.lo1.cells('b').progressOff();
}

//call query version
function fmg_enterQueryv(e) {
	if (e.keyCode==13) {
		fmg.vquery_versionname = $.trim($("#sr_versionname").val());
		fmg.vquery_versiondesc = $.trim($("#sr_versiondesc").val());
		fmg_toPagev(1, 10);
	}
}

//stop event propagation
function fmg_stopEvent(e){
	if (e && e.stopPropagation!=undefined) {
		e.stopPropagation();
	} else if (window.event){
		window.event.cancelBubble = true;
	}
	return false;
}

//返回
function returnValue(rid) {
	var newval = {};
	var frid = fmg.formgrid.getSelectedRowId();
	var rid = fmg.vergrid.getSelectedRowId();
	if (rid==null) {
		msg(fmg_i18n_msg_plsselformver);return;
	}
	newval.formid = fmg.formgrid.cells(frid, fmg.formgrid.getColIndexById("FORMID")).getValue();
	newval.formname = fmg.formgrid.cells(frid, fmg.formgrid.getColIndexById("FORMNAME")).getValue();
	newval.versionid = fmg.vergrid.cells(rid, fmg.vergrid.getColIndexById("VERSIONID")).getValue();
	newval.versionname= fmg.vergrid.cells(rid, fmg.vergrid.getColIndexById("VERSIONNAME")).getValue();
	newval.realValue = newval.formid + ":"+ newval.versionid;
	newval.displayValue = newval.formname + " : " + newval.versionname;
	return [newval];
}
